var Ljs = Ljs || {};
/*
parser={};//name space
parser.JS=function(txt){//构造函数
  var x,y;//private field;
  function m(){};//private method
}
parser.JS.prototype={//类方法
}
*/
var Gate={
	/**
	 * @param txt 模板
	 * @param opt 数据
	 * @return 文本
	 */
	parseText : function(txt,data){
		document.write(txt+'<br>'+data);
		document.close();
	},
	getData : function(href,xhr){
		
	},
	render : function(tplhref,datahref,fn){
		var stamp=Date.parse(new Date());
		tplhref=tplhref+'?'+stamp;
		datahref=datahref+'?' + stamp;
		alert(tplhref);
		alert(datahref);
		var ready1=false,ready2=false;
		var txt;
		var opt;
		Request.doGet(tplhref,function(xhr){
			txt=xhr.responseText;
			alert(txt);
			ready1=true;
			if(ready1 && ready2){
				fn(txt,opt);
			}
		},function(xhr){
			alert('fail to get '+tplhref);
		});
		Request.doGet(datahref,function(xhr){
			opt=xhr.responseText;
			ready2=true;
			if(ready1 && ready2){
				fn(txt,opt);
			}
		},function(xhr){
			alert('fail to get '+datahref);
		});
	}
}

var Cookie = {
	get : function(check_name){
			// first we'll split this cookie up into name/value pairs
		// note: document.cookie only returns name=value, not the other components
		var a_all_cookies = document.cookie.split( ';' );
		var a_temp_cookie = '';
		var cookie_name = '';
		var cookie_value = '';
		var b_cookie_found = false; // set boolean t/f default f
		
		for ( i = 0; i < a_all_cookies.length; i++ )
		{
			// now we'll split apart each name=value pair
			a_temp_cookie = a_all_cookies[i].split( '=' );
			
			
			// and trim left/right whitespace while we're at it
			cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		
			// if the extracted name matches passed check_name
			if ( cookie_name == check_name )
			{
				b_cookie_found = true;
				// we need to handle case where cookie has no value but exists (no = sign, that is):
				if ( a_temp_cookie.length > 1 )
				{
					cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
				}
				// note that in cases where cookie is initialized but no value, null is returned
				return cookie_value;
				break;
			}
			a_temp_cookie = null;
			cookie_name = '';
		}
		if ( !b_cookie_found )
		{
			return null;
		}
	},
	set : function( name, value, expires, path, domain, secure){
		// set time, it's in milliseconds
		var today = new Date();
		today.setTime( today.getTime() );
		
		/*
		if the expires variable is set, make the correct 
		expires time, the current script below will set 
		it for x number of days, to make it for hours, 
		delete * 24, for minutes, delete * 60 * 24
		*/
		if ( expires )
		{
		expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
		
		document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );

	},
	remove : function( name, path, domain){
		if ( Get_Cookie( name ) ) document.cookie = name + "=" +
		( ( path ) ? ";path=" + path : "") +
		( ( domain ) ? ";domain=" + domain : "" ) +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
		
	}
}

function URLUtils() {  
}  
  
URLUtils.getParam = function(url, name) {  
    var   reg = new RegExp("(^|\\?|&)"+name+"=([^&]*)(\\s|&|$)","i");  
    return (reg.test(url))?unescape(RegExp.$2):''; 
}

URLUtils.replaceParam = function(url, name,value) {  
    var   reg = new RegExp("(^|\\?|&)"+name+"=([^&]*)(\\s|&|$)","i");  
    return (reg.test(source))?unescape(RegExp.$2):''; 
}

var Request = {
 getParam : function(val) {
	 var uri = window.location.search;
	 var re = new RegExp("" +val+ "\=([^\&\?]*)", "ig");
	 return ((uri.match(re))?(uri.match(re)[0].substr(val.length+1)):null);
 },
 queryString : function() {
	 var uri = window.location.search;
	 var re = /\w*\=([^\&\?]*)/ig;
	 var retval=[];
	 while ((arr = re.exec(uri)) != null)
	 retval.push(arr[0]);
	 return retval;
 },
 setParam : function(val1, val2) {
	 var a = this.queryString();
	 var retval = "";
	 var seted = false;
	 var re = new RegExp("^" +val1+ "\=([^\&\?]*)$", "ig");
	 for(var i=0; i<a.length; i++) {
	 if (re.test(a[i])) {
	 seted = true;
	 a[i] = val1 +"="+ val2;
	 }
	 }
	 retval = a.join("&");
	 return "?" +retval+ (seted ? "" : (retval ? "&" : "") +val1+ "=" +val2);
 },
 getXHR: function(){
		return (window.XMLHttpRequest) ? new XMLHttpRequest() : ((window.ActiveXObject) ? new ActiveXObject('Microsoft.XMLHTTP') : false);
 },
 /**
  * @param method
  * @param url
  * @param data
  * @param onSuccess
  * @param onfail
  */
 doHttp: function(method,url,data,onSuccess,onfail){
// 	this.xhr = this.xhr || getXHR();
 	var xhr = this.getXHR();
 	xhr.onreadystatechange=function(){
 		if (xhr.readyState == 4) {
	       if (xhr.status == 200) {
	       	 	if(onSuccess)
	       	   		onSuccess(xhr);
	       } else if(onfail){
	         	onfail(xhr);
	       }
		}
 	}
 	xhr.open(method,url,true);
 	xhr.send(data);
 },
 doGet: function(url,onSuccess,onfail){
 	this.doHttp("GET",url,null,onSuccess,onfail);
 },
 doPost: function(url,data,onSuccess,onfail){
 	this.doHttp("POST",url,data,onSuccess,onfail);
 },
 getParams: function(){
	var pos,str,para,parastr,tempstr1;
	var params={};
	tempstr="";
	str = window.location.href;
	pos = str.indexOf("?")
	parastr = unescape(str.substring(pos+1));
	if (str.indexOf("&")>0){
		para = parastr.split("&");
		for(i=0;i<para.length;i++)
		{
			tempstr1 = para[i];
			pos = tempstr1.indexOf("=");
			params[tempstr1.substring(0,pos)]=tempstr1.substring(pos+1);
		}
	}
	return params;
 }
}

Ljs.closure=(function(){//closur
	var fns=[];
	/**
	 * onDomReady
	 */
	window.onDomReady = function(fn) {
		fns.push(fn);
	}
	function runFns(){
		for(var i=0;i<fns.length;i++){
			fns[i]();
		}
	}
	//W3C
	if(document.addEventListener){
		document.addEventListener("DOMContentLoaded", runFns, false);
	}	
	else { //IE
		document.onreadystatechange = function(){	
			if(document.readyState == "interactive")
			{
				runFns();
			}
		}
	}
})();

File=function(){
	
}

File.prototype={
	getLinks : function(){
		
	}
}

Parser=function(){
	
}

Parser.prototype={
	
}

JsParser=function(){
}

JsParser.prototype=new Parser();

JavaParser=function(){
	
}

JavaParser.prototype=new Parser();

function $PARSE(src,data){
	var jct=new jCT(src);
	return jct.Load(data);
}

function $GATE(id,src,data){
	var out=$JCT(src,data);
	$ID(id).innerHTML=out;
}

function $GATE(e,data){
	if(!e) return;
	var el=e;
	if((typeof e)=='string'){
		el=$ID(e);
	}
	var src = el.innerHTML;
	alert("src"+src);
	var out = $JCT(src,data);
	alert("out "+out);
	return el.innerHTML=out;
}
